package zhieasy.cn.xpu_helper.controller.found;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import zhieasy.cn.common.utils.result.ResultUtils;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.common.WechatUser;
import zhieasy.cn.model.found.FoundCommentRely;
import zhieasy.cn.model.found.FoundCommentRoot;
import zhieasy.cn.service.found.FoundCommentRelyService;
import zhieasy.cn.service.found.FoundCommentRootService;

import java.util.List;

/**
 * @author wang zeya
 * @date 2020/8/30 16:53
 */
@RestController
@RequestMapping("/found/common")
@Api(tags = "失物招领模块，评论相关")
public class FoundCommentController {

    @Autowired
    private FoundCommentRootService foundCommentRootService;

    @Autowired
    private FoundCommentRelyService foundCommentRelyService;

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ApiOperation(value = "给帖子增加根评论", notes = "就是一级评论，评论的评论叫做二级评论")
    public ResultVO addRootCommon(@ApiParam("失物招领评论类") @RequestBody FoundCommentRoot foundCommentRoot) {
        foundCommentRoot.setCreateTime(System.currentTimeMillis());
        foundCommentRoot.setValid(1);
        foundCommentRoot.setUpdateTime(System.currentTimeMillis());
        foundCommentRoot.setType(1);
        foundCommentRoot.setLikeNum(0L);
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        //UserDetails userDetails = (UserDetails)auth.getPrincipal();
        foundCommentRoot.setFromId(WechatUser.getWechatUserOpenIdFromSecurityContext());
        boolean idSave = foundCommentRootService.save(foundCommentRoot);
        if (idSave) {
            return ResultUtils.success();
        }
        return ResultUtils.error("评论添加失败");
    }


    @RequestMapping(value = "/{rootCommonId}", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除根评论", notes = "删除根评论，也就是一级评论")
    public ResultVO deleteRootCommon(@ApiParam("根评论的id") @PathVariable("rootCommonId") long rootCommonId) {
        boolean b = foundCommentRootService.removeById(rootCommonId);
        if (b) {
            return ResultUtils.success();
        }
        return ResultUtils.error();
    }

    @RequestMapping(value = "/{lafId}", method = RequestMethod.GET)
    @ApiOperation(value = "获取评论", notes = "获取某个帖子的评论")
    public ResultVO getRootCommonByFoundId(@ApiParam("要获取评论的帖子的id") @PathVariable("lafId") String lafId,
                                           @ApiParam("页数") @RequestParam("pageIndex") int pageIndex,
                                           @ApiParam("每页要多少数据") @RequestParam("pageSize") int pageSize) {

        List<FoundCommentRoot> foundCommentRootList = foundCommentRootService.getCommonRootByFoundId(lafId, pageIndex, pageSize);
        return ResultUtils.success(foundCommentRootList);
    }

    @RequestMapping(value = "/reply", method = RequestMethod.POST)
    @ApiOperation(value = "增加二级评论", notes = "评论的评论叫做二级评论，评论的评论叫做二级评论")
    public ResultVO addReplyCommon(@ApiParam("失物招领评论的子类类") @RequestBody FoundCommentRely foundCommentRely) {
        foundCommentRely.setCreateTime(System.currentTimeMillis());
        foundCommentRely.setValid(1);
        foundCommentRely.setFromId(WechatUser.getWechatUserOpenIdFromSecurityContext());
        foundCommentRely.setUpdateTime(System.currentTimeMillis());
        boolean idSave = foundCommentRelyService.save(foundCommentRely);
        if (idSave) {
            return ResultUtils.success();
        }
        return ResultUtils.error("评论添加失败");
    }

    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    @ApiOperation(value = "增加二级评论", notes = "评论的评论叫做二级评论")
    public ResultVO addCommonToRoot(@ApiParam("失物招领评论类") @RequestBody FoundCommentRely foundCommentRely) {
        foundCommentRely.setCreateTime(System.currentTimeMillis());
        foundCommentRely.setValid(1);
        foundCommentRely.setFromId(WechatUser.getWechatUserOpenIdFromSecurityContext());
        foundCommentRely.setUpdateTime(System.currentTimeMillis());
        boolean idSave = foundCommentRelyService.save(foundCommentRely);
        if (idSave) {
            return ResultUtils.success();
        }
        return ResultUtils.error("评论添加失败");
    }


}
